package com.hawk.eye.client.async.task;

import com.fasterxml.jackson.core.type.TypeReference;
import com.hawk.eye.client.notify.event.RequestFailEvent;
import com.hawk.eye.client.notify.publisher.HawkEyePublisher;
import com.hawk.eye.client.toolkit.ServiceMetadataHolder;
import com.hawk.eye.common.constant.RequestUrlConstant;
import com.hawk.eye.common.core.ApiResult;
import com.hawk.eye.common.encipher.MessageEncipher;
import com.hawk.eye.common.model.ServiceAuthData;
import com.hawk.eye.common.model.ServiceMetadata;
import com.hawk.eye.common.client.IClient;
import com.hawk.eye.client.client.RequestParam;
import com.hawk.eye.common.utils.JsonUtil;

/**
 * @className: RequestServerTask
 * @description: 用于请求HawkEyeServer的任务
 * @author: salad
 * @date: 2023/9/16
 **/
public class RequestServerTask implements HawkEyeTask {

    private final IClient client;

    private final ServiceMetadata serviceMetadata;

    private final String hawkEyeServerAddress;

    private final MessageEncipher messageEncipher;

    private final Boolean firstConnect;

    public RequestServerTask(IClient client, ServiceMetadata serviceMetadata,
                             String hawkEyeServerAddress, MessageEncipher messageEncipher,
                             Boolean firstConnect) {
        this.client = client;
        this.serviceMetadata = serviceMetadata;
        this.hawkEyeServerAddress = hawkEyeServerAddress;
        this.messageEncipher = messageEncipher;
        this.firstConnect = firstConnect;
    }

    public RequestServerTask(IClient client, String hawkEyeServerAddress,
                             MessageEncipher messageEncipher, Boolean firstConnect) {
        this.client = client;
        this.serviceMetadata = ServiceMetadataHolder.getServiceMetadata();
        this.hawkEyeServerAddress = hawkEyeServerAddress;
        this.messageEncipher = messageEncipher;
        this.firstConnect = firstConnect;
    }

    @Override
    public void run() {
        try {
            //请求参数
            RequestParam requestParam = new RequestParam();
            //检查加密传输模式是否开启
            if (messageEncipher.enabled()) {
                this.serviceMetadata.setServiceName(messageEncipher.encrypt(this.serviceMetadata.getServiceName()));
                this.serviceMetadata.setToken(messageEncipher.encrypt(this.serviceMetadata.getToken()));
            }
            requestParam.setServiceMetadata(this.serviceMetadata);
            //开始请求HawkEyeServer
            String json = client.pushMetadata(RequestUrlConstant.PUSH_SERVICE_METADATA_URI
                    .formatUri(hawkEyeServerAddress), JsonUtil.toJson(requestParam));
            ApiResult<ServiceAuthData> result = JsonUtil.parse(json, new TypeReference<ApiResult<ServiceAuthData>>() {
            });
            //拿到返回数据后进行事件发布
            HawkEyePublisher.publishEvent(result, firstConnect);
        } catch (Throwable e) {
            //建立连接报错后，需要发布一个连接失败事件
            HawkEyePublisher.publish(new RequestFailEvent(ApiResult.fail()));
        }
    }

}
